import {
  Body,
  Controller,
  Get,
  Post,
  Query,
  Header,
  Res,
  HttpException,
  HttpStatus,
  HttpCode,
  Render,
} from "@nestjs/common";
import { WechatService } from "./wechat.service";
import { IWonewSer } from "@/typing";
import { XML } from "@/core/decorators/xml.decorator";

@Controller("wechat")
export class WechatController {
  constructor(private readonly wechatService: WechatService) {}

  @Get("callback")
  verify(@Query() query: IWonewSer.WechatVerifyParams) {
    return this.wechatService.verifySign(query);
  }

  @HttpCode(200)
  @Post("callback")
  handleMessage(@XML() message: IWonewSer.WechatMessage) {
    return this.wechatService.buildReply(message);
  }

  @Get("auth2")
  @Render("wechat/test")
  auth2(@Query() query: { code: string }) {
    return this.wechatService.auth2(query);
  }

  @Get("unauth")
  @Render("wechat/unauth")
  unauth() {
    return {
      time: Date.now(),
      uri: this.wechatService.getAuthUri(
        "https://dragon-dev.frp.wonew.top/wechat/unauth",
      ),
    };
  }
}
